from django.http import HttpResponse
from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet

from api.serializers import CollegeSerializer, CollegeSearchSerializer, GradeSerializer, GradeSearchSerializer
from common.models import Student, College, Grade


def get_student(request):
    if request.method == "GET":
        student_list = Student.objects.all()
        return render(request, "student_info.html", {"stu_list": student_list})


def add_student(request):
    if request.method == "GET":
        grade_list = Grade.objects.all()
        college_list = College.objects.all()

        return render(request, 'add_student.html', {"grade_list": grade_list, "college_list": college_list})

    if request.method == "POST":
        stuname = request.POST.get('stuname')
        gender = request.POST.get("gender")
        stutel = request.POST.get('stu_tel')
        grade = request.POST.get('grade')
        grade_G = Grade.objects.get(grade_id=grade)
        college = request.POST.get('college')
        college_C = College.objects.get(coll_id=college)

        if not Student.objects.filter(stu_tel=stutel).exists():
            Student.objects.create(stu_name=stuname,
                                   gender=gender,
                                   stu_tel=stutel,
                                   grade=grade_G,
                                   collage=college_C)
            code = {"status": 200, "msg": "添加成功"}
            return render(request, 'success.html', {"code": code})
        else:
            error = "电话号码重复"
            return render(request, 'add_student.html', {"error": error})


def del_student(request):
    sid = request.GET.get('sid')
    Student.objects.filter(stu_id=sid).delete()
    return render(request, 'student_info.html')


def edit_student(request):
    if request.method == "GET":

        sid = request.GET.get('sid')
        obj = Student.objects.filter(stu_id=sid).first()
        grade_list = Grade.objects.all()
        college_list = College.objects.all()

        return render(request, 'edit_student.html', {"obj": obj, "grade_list": grade_list,
                                                     "college_list": college_list})

    elif request.method == "POST":
        sid = request.GET.get('sid')
        stuname = request.POST.get('stuname')
        gender = request.POST.get("gender")
        grade = request.POST.get('grade')
        grade_G = Grade.objects.get(grade_id=grade)
        college = request.POST.get('college')
        college_C = College.objects.get(coll_id=college)

        Student.objects.filter(stu_id=sid).update(stu_name=stuname,
                               gender=gender,

                               grade=grade_G,
                               collage=college_C)
        code = {"status": 200, "msg": "添加成功"}
        return render(request, 'success.html', {"code": code})

    else:
        return render(request, 'student_info.html')


class CollegeViewSet(ModelViewSet):
    queryset = College.objects.all()
    serializer_class = CollegeSearchSerializer

    def get_serializer_class(self):
        if self.action in ("create", "update"):
            return CollegeSerializer
        return self.serializer_class


class GradeViewSet(ModelViewSet):
    queryset = Grade.objects.all()
    serializer_class = GradeSearchSerializer

    def get_serializer_class(self):
        if self.action in ("create", "update"):
            return GradeSerializer
        return self.serializer_class
